<?php

namespace App\Models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Blind extends Model
{
    use HasFactory;

    protected $fillable = ['name', 'description', 'url', 'price', 'admin_id', 'blind_cate_id', 'order', 'hidden', 'blind_type', 'type', 'tend_time','stock_quantity'];
    // public $timestamps = true;
    // protected $dateFormat = 'U';
    protected $casts = [
        'created_at' => 'datetime:Y-m-d h:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s',
        'deleted_at' => 'datetime:Y-m-d H:i:s',
        'publish_at' => 'datetime:Y-m-d H:i:s',
    ];
    /**
     * Prepare a date for array / JSON serialization.
     *
     * @param  \DateTimeInterface  $date
     * @return string
     */
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }
    public function admins()
    {
        return $this->belongsTo(Admin::class, 'admin_id');
    }

    public function blindcates()
    {
        return $this->belongsTo(BlindCate::class, 'blind_cate_id');
    }

    public function blindshops()
    {
        return $this->belongsToMany(Shop::class, 'blind_shops', 'blind_id', 'shop_id')->withPivot('rate')->withTimestamps();
    }

    public function statuslabel()
    {
        return $this->belongsTo(Dicdata::class, 'status', 'value')
            ->where('typeCode', '=', 'blind_status');
    }

    public function blindtypelabel()
    {
        return $this->belongsTo(Dicdata::class, 'blind_type', 'value')
            ->where('typeCode', '=', 'blind_type');
    }

    public function typelabel()
    {
        return $this->belongsTo(Dicdata::class, 'type', 'value')
            ->where('typeCode', '=', 'reward_type');
    }
}
